{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ECSimpleSnow component\n",
    "\n",
    "ECSimpleSnow is an empirical algorithm to melt snow according to the surface temperature and increase snow depth according to the precipitation that has fallen since the last time step.\n",
    "\n",
    "## Details: \n",
    "\n",
    "**Brown, R. D., Brasnett, B., & Robinson, D. (2003). Gridded North American monthly snow depth and snow water equivalent for GCM evaluation. Atmosphere-Ocean, 41(1), 1-14.**\n",
    "\n",
    "**URL:** https://www.tandfonline.com/doi/abs/10.3137/ao.410101\n",
    "\n",
    "## Source code in Fortran:\n",
    "\n",
    "**URL:** https://github.com/permamodel/Snow_BMI_Fortran\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### load module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.optimize import curve_fit\n",
    "from tqdm import tqdm\n",
    "\n",
    "import pymt.models\n",
    "ec = pymt.models.ECSimpleSnow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### load example configuration and inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_file, config_dir = ec.setup('.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### initialize by using default example data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ec.initialize(config_file, config_dir)\n",
    "ec.set_value('snow_class', 2)\n",
    "ec.set_value('open_area_or_not', 1)\n",
    "\n",
    "# List input and output variable names.\n",
    "print(ec.get_output_var_names())\n",
    "print(ec.get_input_var_names())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Implement the simple snow model for the first year as an example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=[4,9])\n",
    "h0 = plt.subplot(3,1,1)\n",
    "h1 = plt.subplot(3,1,2)\n",
    "h2 = plt.subplot(3,1,3)\n",
    "\n",
    "h0.title.set_text('Snow Depth')\n",
    "h1.title.set_text('Snow Density')\n",
    "h2.title.set_text('Air Temperature')\n",
    "\n",
    "print('Air Temperature Unit:', ec.var_units('land_surface_air__temperature'))\n",
    "print('Snow Depth Unit:'     , ec.var_units('snowpack__depth'))\n",
    "print('Snow Density Unit:'   , ec.var_units('snowpack__mass-per-volume_density'))\n",
    "\n",
    "for i in tqdm(np.arange(365)):\n",
    "\n",
    "    ec.update()\n",
    "    \n",
    "    tair  = ec.get_value('land_surface_air__temperature')    \n",
    "    snd   = ec.get_value('snowpack__depth', units='m')\n",
    "    rsn   = ec.get_value('snowpack__mass-per-volume_density')\n",
    "    \n",
    "    units = ec.var_units('snowpack__depth')\n",
    "    \n",
    "    h0.scatter(ec.time, snd, c='k')    \n",
    "    h1.scatter(ec.time, rsn, c='k')\n",
    "    h2.scatter(ec.time,tair, c='k')\n",
    "    \n",
    "    \n",
    "# ec.finalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Comparison with Observations at Barrow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Comparison](https://github.com/permamodel/Snow_BMI_Fortran/blob/master/data/Barrow.png?raw=true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
